package lanou.giftsay_01.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

import android.widget.TextView;
import android.widget.Toast;

import lanou.giftsay_01.R;
import lanou.giftsay_01.bean.DanpinBean;
import lanou.giftsay_01.main.categorymain.GiftListActivity;
import lanou.giftsay_01.tools.NoScrollGridView;


/**
 * Created by dllo on 16/8/19.
 */
public class RightAdapter extends BaseAdapter{
    private DanpinBean danpinBean ;
    private Context context ;


    public void setDanpinBean(DanpinBean danpinBean) {
        this.danpinBean = danpinBean;
        notifyDataSetChanged();
    }

    public RightAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return danpinBean!= null ? danpinBean.getData().getCategories().size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_right, parent , false);
        }
        final TextView title = (TextView) convertView.findViewById(R.id.right_tv_title);
        title.setVisibility(View.VISIBLE);
        title.setText(danpinBean.getData().getCategories().get(position).getName());
        NoScrollGridView nsgv = (NoScrollGridView) convertView.findViewById(R.id.nsgv_item);
        NsgvAdapte nsgvAdapter = new NsgvAdapte(context);
        nsgvAdapter.setDanpinBean(danpinBean);
        nsgvAdapter.setI(position);
        nsgv.setAdapter(nsgvAdapter);
        final int y = nsgvAdapter.getI();
        nsgv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(context,GiftListActivity.class);
                intent.putExtra("id",String.valueOf(danpinBean.getData().getCategories().get(y).getSubcategories().get(position).getId()));
                intent.putExtra("name" , danpinBean.getData().getCategories().get(y).getSubcategories().get(position).getName());
                Log.d("RightAdapter", "danpinBean.getData().getCategories().get(y).getSubcategories().get(position).getId():" + danpinBean.getData().getCategories().get(y).getSubcategories().get(position).getId());
                context.startActivity(intent);
            }
        });
        return convertView;
    }
}
